<?php

class Create_Numbers_Table {
    private $tableName = 'numbers';
	/**
	 * Make changes to the database.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create($this->tableName, function ($tbl){
            $tbl->increments('id');
            $tbl->integer('collection_id')->unsigned();
            $tbl->integer('number');

            $tbl->foreign('collection_id')->references('id')->on('collections');
        });

        $initData = require path('storage') . 'cache/built-in-collections.php';
        foreach ($initData as $curCollection) {
            $curCollection['collection']['built_in'] = true;
            $collectionId = DB::table('collections')->insert_get_id($curCollection['collection']);
            foreach ($curCollection['numbers'] as $curNumber) {
                DB::table($this->tableName)->insert(array('collection_id' => $collectionId, 'number' => $curNumber));
            }
        }
	}

	/**
	 * Revert the changes to the database.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop($this->tableName);
	}

}